package io.gitee.wiqer.easy;

import java.util.ArrayList;

public class SolutionNC38_SpiralOrder {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> list = new ArrayList<>();

        if(matrix.length == 0) {
            return list;
        }

        int left = 0;
        int right = matrix[0].length - 1;
        int top = 0;
        int bottom = matrix.length - 1;
        int x = 0;


        while(true) {
            for(int i = left; i <= right; i++) {  //从左到右
                list.add(matrix[top][i]) ;
            }

            if(++top > bottom){
                break;
            }
            for(int i = top; i <= bottom; i++){
                list.add( matrix[i][right]);    //从上到下
            }

            if(left > --right){
                break;
            }
            for(int i = right; i >= left; i--){
                list.add(matrix[bottom][i]); //从右到左
            }

            if(top > --bottom){
                break;
            }
            for(int i = bottom; i >= top; i--){
                list.add(matrix[i][left]);   //从下到上
            }

            if(++left > right){
                break;
            }
        }
        return list;
    }
}
